﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using DataAccess;

namespace DataService
{
    public class ArticlesService: ServiceBase<Article>, IArticles
    {
        private UsersArticleService _usersArticles;

        public ArticlesService(string connectionString):base(connectionString)
        {
            _usersArticles = new UsersArticleService(connectionString);
        }

        #region Implementation of IArticles

        public Article AddArticleWithAutor(Article article, string name)
        {
            aspnet_User user;

            var usersArticle = new UsersArticle();

            var createdArticle = CreateItem(article);

            using (var dataContext = new DataContext(_connectionString))
            {
                var table = dataContext.GetTable<aspnet_User>();
                user = table.Where(item => item.UserName == name).First();
            }
            
            usersArticle.ArticleID = article.ArticleID;
            usersArticle.UserID = user.UserId;

            var usersArticleService = new UsersArticleService(_connectionString);

            usersArticleService.CreateItem(usersArticle);

            return createdArticle;
        }

        public Article GetArticle(int articleId)
        {
            return GetRecord(articleId);
        }

        public void UpdateArticle(Article article)
        {
            UpdateItem(article);
        }

        public void RemoveArticle(int id)
        {
            Article article = GetRecord(id);
            bool removeItem = RemoveItem(id);

            if(!removeItem)
                throw new Exception(string.Format("Article {0} wasn't removed.",article.ArticleName));
        }

        public List<Article> GetAuthorArticles(string authorName)
        {
            aspnet_User user;

            using (var dataContext = new DataContext(_connectionString))
            {
                var table = dataContext.GetTable<aspnet_User>();
                user = table.Where(item => item.UserName == authorName).First();

                if (user == null)
                    return null;

                List<UsersArticle> usersArticles =
                    dataContext
                        .GetTable<UsersArticle>()
                        .Where(record => record.UserID == user.UserId).ToList();

                /*selects articles from Articles where articleID in usersArticles.ArticleID*/
                return dataContext.GetTable<Article>().ToList()
                    .Where(article => usersArticles
                                          .Any(usersArticle => usersArticle.ArticleID == article.ArticleID))
                    .ToList();
            }
        }

        public bool AddAuthorToArticle(int articleId, string authorName)
        {
            var usersArticle = new UsersArticle();

            var userService = new UsersService(_connectionString);

            aspnet_User user = userService.GetUserByName(authorName);

            if (user == null)
                return false;
          
            usersArticle.ArticleID = articleId;
            usersArticle.UserID = user.UserId;

            var usersArticlesService = new UsersArticleService(_connectionString);

            usersArticlesService.CreateItem(usersArticle);
            
            return true;
        }

        public bool RemoveAuthorFromArticle(int articleId, string authorName)
        {
            var usersArticle = new UsersArticle();

            var userService = new UsersService(_connectionString);

            aspnet_User user = userService.GetUserByName(authorName);

            if (user == null)
                return false;

            usersArticle.ArticleID = articleId;
            usersArticle.UserID = user.UserId;

            var usersArticlesService = new UsersArticleService(_connectionString);

            usersArticlesService.DeleteItem(usersArticle);

            return true;
        }

        public List<Article> GetAllArticles()
        {
            return GetAllItems();
        }

        #endregion

        protected override Article GetRecord(int itemID, DataContext dataContext)
        {
            return dataContext
                .GetTable<Article>()
                .Where(item => item.ArticleID == itemID)
                .FirstOrDefault();
        }
    }
}
